package com.bezdelnick.forum.web.page;

import com.bezdelnick.forum.repository.FileSystemFolderRepository;
import com.bezdelnick.forum.util.PropertyUtils;
import com.bezdelnick.forum.web.resource.ImageFromFileSystemReference;
import org.apache.wicket.PageReference;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.upload.FileUpload;
import org.apache.wicket.markup.html.form.upload.MultiFileUploadField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.util.file.File;
import org.apache.wicket.util.file.Folder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 05.09.12
 * Time: 9:46
 * To change this template use File | Settings | File Templates.
 */
public class UploadAndInsertContentPage extends WebPage {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadAndInsertContentPage.class);

    //    private List<String> files = new ArrayList<String>();
    private final Collection<FileUpload> uploads = new ArrayList<FileUpload>();

    private String fileName;
    private String filePath;
    private String resourceUrl;
    private String displayMessage;

    public UploadAndInsertContentPage(final PageReference modalWindowPage, final ModalWindow window) {
        //
        Form form = new Form("upload_form") {
            @Override
            protected void onSubmit() {
                super.onSubmit();
                for (FileUpload upload : uploads) {
                    FileSystemFolderRepository.get().addFile(upload);
                    fileName = upload.getClientFileName();
                }

                if (fileName != null) {
                    resourceUrl = FileSystemFolderRepository.get().getFileUrlByFileName(fileName);
                    if (resourceUrl != null)
                        displayMessage = "File is loaded successfully!";
                }
            }
        };
        add(form);

        form.add(new MultiFileUploadField("upload_field", new PropertyModel(this, "uploads"), 1));

        Label lblResourceUrl = new Label("resource_url", new PropertyModel<String>(this, "resourceUrl"));
        add(lblResourceUrl);

        Label lblDisplayMessage = new Label("message_field", new PropertyModel<String>(this, "displayMessage"));
        add(lblDisplayMessage);

        add(new AjaxLink<Void>("close_cancel") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                window.close(target);
            }
        });

        add(new AjaxLink<Void>("close_ok") {
            @Override
            public void onClick(AjaxRequestTarget target) {
                if (modalWindowPage != null && resourceUrl != null) {
                    try {
                        ((AddQuestionPage) modalWindowPage.getPage()).setImageUrl(resourceUrl);
                    } catch (Exception e) {
                        LOGGER.info(e.getMessage());
                    }
                    try {
                        ((TestRichTextEditorPage) modalWindowPage.getPage()).setImageUrl(resourceUrl);
                    } catch (Exception e) {
                        LOGGER.info(e.getMessage());
                    }
                }
                window.close(target);
            }
        });
    }
}
